﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class pim_PersonalInfo : System.Web.UI.Page
{
    public ArrayList contactarr = null;
    public ArrayList emergencyarr = null;
    public ArrayList expedientearr = null;
    public int per_id = 0;
    public int emp_id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int tabindex = 0;
        llenopuesto();
        llenocontrato();
        habilitocampos(false);

        if (!Page.IsPostBack)
        {
            llenocompanias();
            ddl_company_SelectedIndexChanged(sender, e);
            if (Request.QueryString["emp_id"] != null) emp_id = int.Parse(Request.QueryString["emp_id"].ToString());
            Session["emp_id"] = emp_id;
            if (Request.QueryString["tabindex"] != null) tabindex = int.Parse(Request.QueryString["tabindex"].ToString());

            TabContainer1.ActiveTabIndex = tabindex;

            RE_GenericBean personainf = DB.getPersonaInf(per_id, emp_id);

            lb_person_id.Text = personainf.intC1.ToString();
            lb_emp_id.Text = personainf.intC2.ToString();
            tb_nombre.Text = personainf.strC1;
            tb_apellido.Text = personainf.strC2;
            tb_apellidocasada.Text = personainf.strC3;
            tb_nit.Text = personainf.strC7;
            ddl_cuenta.SelectedValue = personainf.intC3.ToString();
            ddl_genero.SelectedValue = personainf.strC4;
            ddl_estadocivil.SelectedValue = personainf.strC5;
            tb_fechanacimiento.Text = personainf.strC6;
            tb_igss.Text = personainf.strC8;
            tb_codigoempresa.Text = personainf.strC9;



            contactarr = (ArrayList)DB.getContactArr("where tpd_tper_id=" + lb_person_id.Text);
            emergencyarr = (ArrayList)DB.getEmergencyArr("where tpem_tper_id=" + lb_person_id.Text);
            expedientearr = (ArrayList)DB.getExpedienteArr(int.Parse(lb_emp_id.Text));

            if (emergencyarr != null && emergencyarr.Count > 0)
            {
                foreach (RE_GenericBean rg in emergencyarr)
                {
                    lb_emergency_id_1.Text = rg.intC1.ToString();
                    tb_emename_1.Text = rg.strC1;
                    tb_emerelacion_1.Text = rg.strC2;
                    tb_emeaddr1_1.Text = rg.strC3;
                    tb_emeaddr2_1.Text = rg.strC12;
                    tb_emecity_1.Text = rg.strC4;
                    tb_emestate_1.Text = rg.strC5;
                    tb_emezip_1.Text = rg.strC6;
                    tb_emecountry_1.Text = rg.strC7;
                    tb_emetel_1.Text = rg.strC8;
                    tb_emeemail_1.Text = rg.strC11;

                }

            }


            if (contactarr != null && contactarr.Count > 0)
            {
                foreach (RE_GenericBean rg in contactarr)
                {
                    lb_contactid_1.Text = rg.intC1.ToString();
                    tb_add1.Text = rg.strC1;
                    tb_add2.Text = rg.strC2;
                    tb_city.Text = rg.strC3;
                    tb_state.Text = rg.strC4;
                    tb_zip.Text = rg.strC5;
                    tb_country.Text = rg.strC6;
                    tb_tel.Text = rg.strC7;
                    tb_celular.Text = rg.strC8;
                    tb_email.Text = rg.strC9;

                }
            }

            if (expedientearr != null && expedientearr.Count > 0)
            {
                foreach (RE_GenericBean rg in expedientearr)
                {
                    tb_base_salary.Text = rg.decC1.ToString();
                    tb_bonus.Text = rg.decC2.ToString();
                    tb_commision.Text = rg.decC3.ToString();
                    tb_other.Text = rg.decC4.ToString();
                    tb_hired_date.Text = rg.strC4;
                    tb_end_date.Text = rg.strC5;
                    lb_expediente_id_1.Text = rg.intC1.ToString();

                }
            }
        }
        else { 
            emp_id= (int)Session["emp_id"];
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        RE_GenericBean personalinf = new RE_GenericBean();
        bool actualiza=false;

        personalinf.intC1 = int.Parse(lb_person_id.Text);
        personalinf.intC2 = int.Parse(lb_emp_id.Text);
        personalinf.strC1 = tb_nombre.Text.Trim();
        personalinf.strC2 = tb_apellido.Text.Trim();
        personalinf.strC3 = tb_apellidocasada.Text.Trim();
        personalinf.strC4 = tb_nit.Text.Trim();
        personalinf.strC5 = ddl_genero.SelectedValue.ToUpper();
        personalinf.strC6 = ddl_estadocivil.SelectedValue.ToUpper();
        personalinf.strC7 = tb_fechanacimiento.Text;
        personalinf.strC8 = tb_igss.Text.Trim();
        

        RE_GenericBean contact1 = null;
        if (!tb_add1.Text.Equals(""))
        {
            contact1 = new RE_GenericBean();
            contact1.intC1 = int.Parse(lb_contactid_1.Text);
            contact1.strC1 = tb_add1.Text;
            contact1.strC2 = tb_add2.Text;
            contact1.strC3 = tb_city.Text;
            contact1.strC4 = tb_state.Text;
            contact1.strC5 = tb_zip.Text;
            contact1.strC6 = tb_country.Text;
            contact1.strC7 = tb_tel.Text;
            contact1.strC8 = tb_celular.Text;
            contact1.strC9 = tb_email.Text;
            contact1.strC10 = tb_personalemail.Text;
        }

        RE_GenericBean contact2 =null;
        //if (!tb_add1_1.Text.Equals(""))
        //{
        //    contact2 = new RE_GenericBean();
        //    contact2.intC1 = int.Parse(lb_contactid_2.Text);
        //    contact2.strC1 = tb_add1_1.Text;
        //    contact2.strC2 = tb_add2_1.Text;
        //    contact2.strC3 = tb_city_1.Text;
        //    contact2.strC4 = tb_state_1.Text;
        //    contact2.strC5 = tb_zip_1.Text;
        //    contact2.strC6 = tb_country_1.Text;
        //    contact2.strC7 = tb_tel_1.Text;
        //    contact2.strC8 = tb_celular_1.Text;
        //    contact2.strC9 = tb_email_1.Text;
        //    contact2.strC10 = tb_personalemail_1.Text;
        //}

        RE_GenericBean emergency1 = null;
        if (!tb_emename_1.Text.Equals(""))
        {
            emergency1 = new RE_GenericBean();
            emergency1.intC1 = int.Parse(lb_emergency_id_1.Text);
            emergency1.strC1 = tb_emename_1.Text;
            emergency1.strC2 = tb_emerelacion_1.Text;
            emergency1.strC3 = tb_emeaddr1_1.Text;
            emergency1.strC4 = tb_emeaddr2_1.Text;
            emergency1.strC5 = tb_emecity_1.Text;
            emergency1.strC6 = tb_emestate_1.Text;
            emergency1.strC7 = tb_emezip_1.Text;
            emergency1.strC8 = tb_emecountry_1.Text;
            emergency1.strC9 = tb_emetel_1.Text;
            emergency1.strC10 = tb_emeemail_1.Text;
        }
       
        RE_GenericBean emergency2 = null;
        //if (!tb_emename_2.Text.Equals(""))
        //{
        //    emergency2 = new RE_GenericBean();
        //    emergency2.intC1 = int.Parse(lb_emergency_id_2.Text);
        //    emergency2.strC1 = tb_emename_2.Text;
        //    emergency2.strC2 = tb_emerelacion_2.Text;
        //    emergency2.strC3 = tb_emeaddr1_2.Text;
        //    emergency2.strC4 = tb_emeaddr2_2.Text;
        //    emergency2.strC5 = tb_emecity_2.Text;
        //    emergency2.strC6 = tb_emestate_2.Text;
        //    emergency2.strC7 = tb_emezip_2.Text;
        //    emergency2.strC8 = tb_emecountry_2.Text;
        //    emergency2.strC9 = tb_emetel_2.Text;
        //    emergency2.strC10 = tb_emeemail_2.Text;
        //}

        RE_GenericBean jobposition = new RE_GenericBean();
        if (tb_hired_date.Text != null && !tb_hired_date.Text.Equals("")) { 
            jobposition.intC1=int.Parse(ddl_jobtitle.SelectedValue);
            jobposition.intC2 = int.Parse(ddl_status.SelectedValue);
            jobposition.strC1 = tb_hired_date.Text;
            jobposition.strC2 = tb_end_date.Text;
            jobposition.strC3 = tb_start_contract.Text;
            jobposition.strC4 = tb_end_contract.Text;
            jobposition.strC5 = tb_inmediate_sup.Text;
            jobposition.intC3 = int.Parse(ddl_company.SelectedValue);
            jobposition.intC4 = int.Parse(ddl_cuenta.SelectedValue);
            jobposition.strC6 = tb_codigoempresa.Text.Trim().ToUpper();
            
        }

        RE_GenericBean salary = new RE_GenericBean();
        if (tb_base_salary.Text != null && !tb_base_salary.Text.Equals("")) {
            salary.decC1 = decimal.Parse(tb_base_salary.Text);
            salary.decC2 = decimal.Parse(tb_bonus.Text);
            salary.decC3 = decimal.Parse(tb_commision.Text);
            salary.decC4 = decimal.Parse(tb_other.Text);
            salary.decC5 = decimal.Parse(tb_several_amount.Text);
            salary.strC1 = ddl_moneda.SelectedValue;
            salary.strC2 = tb_bank.Text;
            salary.strC3 = tb_account.Text;
            salary.intC2 = int.Parse(ddl_empoyement.SelectedValue);
        }
        if (personalinf.intC1 != 0) actualiza = true;

        RE_GenericBean result = DB.InsertaPersonalInfo(personalinf, contact1, contact2, emergency1, emergency2, jobposition, salary, actualiza);

        if (result != null) { // si es diferente de null es porque si se pudo insertar
            lb_person_id.Text = result.intC1.ToString();
            per_id = result.intC1;
            lb_emp_id.Text = result.intC2.ToString();
        }
       
    }

    private void llenocompanias()
    {
        ArrayList companyarr = null;
        ListItem item = null;
        try
        {
            companyarr = (ArrayList)DB.getArrCompania("");
            if (companyarr != null && companyarr.Count > 0)
            {
                foreach (RE_GenericBean company in companyarr)
                {
                    item = new ListItem(company.strC1, company.intC1.ToString());
                    ddl_company.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    }

    protected void ddl_company_SelectedIndexChanged(object sender, EventArgs e)
    {
        ArrayList cuentasarr = null;
        ListItem item = null;
        try
        {
            cuentasarr = (ArrayList)DB.getArrCuenta("where tcu_tco_id="+ddl_company.SelectedValue);
            if (cuentasarr != null && cuentasarr.Count > 0)
            {
                foreach (RE_GenericBean cuenta in cuentasarr)
                {
                    item = new ListItem(cuenta.strC1, cuenta.intC1.ToString());
                    ddl_cuenta.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    }

    private void llenopuesto()
    {
        ArrayList puestoarr = null;
        ListItem item = null;
        try
        {
            puestoarr = (ArrayList)DB.getArrPuestoEmpleado("");
            if (puestoarr != null && puestoarr.Count > 0)
            {
                foreach (RE_GenericBean puesto in puestoarr)
                {
                    item = new ListItem(puesto.strC1, puesto.intC1.ToString());
                    ddl_jobtitle.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    }

    private void llenocontrato()
    {
        ArrayList contratoarr = null;
        ListItem item = null;
        try
        {
            contratoarr = (ArrayList)DB.getArrTipoContrato("");
            if (contratoarr != null && contratoarr.Count > 0)
            {
                foreach (RE_GenericBean contrato in contratoarr)
                {
                    item = new ListItem(contrato.strC1, contrato.intC1.ToString());
                    ddl_empoyement.Items.Add(item);
                }
            }
        }
        catch (Exception err)
        {
            utility.log(err.StackTrace);
        }
    }
    protected void bt_edit_Click(object sender, EventArgs e)
    {
        emp_id= (int)Session["emp_id"];
        habilitocampos(true);
    }

    private void habilitocampos(bool p)
    {
        tb_nombre.Enabled = p;
        tb_apellido.Enabled = p;
        tb_apellidocasada.Enabled = p;
        tb_account.Enabled = p;
        tb_add1.Enabled = p;
        tb_add2.Enabled = p;
        tb_bank.Enabled = p;
        tb_base_salary.Enabled = p;
        tb_bonus.Enabled = p;
        tb_celular.Enabled = p;
        tb_city.Enabled = p;
        tb_codigoempresa.Enabled = p;
        tb_commision.Enabled = p;
        tb_country.Enabled = p;
        tb_email.Enabled = p;
        tb_emeaddr1_1.Enabled = p;
        tb_emeaddr2_1.Enabled = p;
        tb_emecity_1.Enabled = p;
        tb_emecountry_1.Enabled = p;
        tb_emeemail_1.Enabled = p;
        tb_emename_1.Enabled = p;
        tb_emerelacion_1.Enabled = p;
        tb_emestate_1.Enabled = p;
        tb_emetel_1.Enabled = p;
        tb_emezip_1.Enabled = p;
        tb_end_contract.Enabled = p;
        tb_end_date.Enabled = p;
        tb_fechanacimiento.Enabled = p;
        tb_hired_date.Enabled = p;
        tb_igss.Enabled = p;
        tb_inmediate_sup.Enabled = p;
        tb_nit.Enabled = p;
        tb_other.Enabled = p;
        tb_personalemail.Enabled = p;
        tb_several_amount.Enabled = p;
        tb_start_contract.Enabled = p;
        tb_state.Enabled = p;
        tb_tel.Enabled = p;
        tb_zip.Enabled = p;
        ddl_company.Enabled = p;
        ddl_cuenta.Enabled = p;
        ddl_empoyement.Enabled = p;
        ddl_estadocivil.Enabled = p;
        ddl_genero.Enabled = p;
        ddl_jobtitle.Enabled = p;
        ddl_moneda.Enabled = p;
        ddl_status.Enabled = p;
        
    }
    protected void bt_cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("PersonalList.aspx");
    }
    protected void bt_delete_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["emp_id"] != null) emp_id = int.Parse(Request.QueryString["emp_id"].ToString());
        
        if (emp_id != 0)
        {
           
            HyperLink1_ModalPopupExtender.Show();
           
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
      //  int result = DB.DeleteEmergencyContact(int.Parse(lb_emergency_id_1.Text));
       int result2 = DB.DeleteContactInfo(int.Parse(lb_contactid_1.Text));
        //int result3 = DB.DeleteExpedienteInfo(int.Parse(lb_expediente_id_1.Text));
        int result4 = DB.DeletePersona(int.Parse(lb_person_id.Text));
        int result5 = DB.DeleteEmployee(int.Parse(lb_emp_id.Text));
        
        Response.Redirect("PersonalList.aspx");
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        emp_id= (int)Session["emp_id"];
        Response.Redirect("PersonalInfo.aspx?emp_id="+emp_id);
    }
}